'''
Created on Feb 15, 2012

@author: oabalbin
'''
import logging
#import argparse
import os
import sys
import glob
from optparse import OptionParser
from collections import defaultdict,deque
from exome.jobs.job_runner import qsub_cac, qsub_loc, run_local
from exome.jobs.config import ExomePipelineConfig, ExomeAnalysisConfig


# define base paths to job scripts
_jobs_dir = os.path.abspath(os.path.dirname(__file__))

# Global Variables
NODE_MEM=45000.0
NODE_CORES=12
SINGLE_CORE=1
MEM_PER_CORE= int(float(NODE_MEM) / NODE_CORES)
# wt=walltime
WT_SHORT= "24:00:00"
WT_LONG= "48:00:00"#"60:00:00" #"100:00:00"


def check_create_dir(root_path, dir_name=None):
    if not os.path.isdir(root_path):
            os.mkdir( root_path )
    if dir_name is not None:
        subfolder=os.path.join(root_path,dir_name)
        if not os.path.isdir(subfolder):
                os.mkdir( subfolder )
    else:             
        subfolder=root_path

    return subfolder


def read_files_folder(folderpath,ext):
    ''' '''
    # Read files in folder
    myfiles=[]
    for infile in glob.glob( os.path.join(folderpath, '*'+ext) ):
        myfiles.append(infile)
        #print "current file is: " + infile
    return myfiles


def snps_calling_multipleSamples(analysis, config, num_processors, jobrunfunc, prev_deps):
    print 'This is function is not implemented yet'
    pass


def snps_calling_pairedSamples(analysis, configrun, num_processors, runfunc, depends, use_recal_files=False):
    '''
    '''
    extra_mem, num_cores = configrun.gatk_use_mem, configrun.gatk_num_cores
    path_to_vcftools, path_to_picard, path_to_sam = configrun.vcftools_path, configrun.picard_path, \
                                                configrun.samtools_path
    path_to_varscan = configrun.varscan_path

    target_exons = configrun.sam_target_exons

    genomes = configrun.genomes['human']
    ref_genome = genomes.bwa_ref_genome
    my_email=configrun.email_addresses
    
    sam_call_parameters=configrun.sam_mpileup_params
    # Hard threshold options
    varscan_options=configrun.varscan_params #map(str,[1,1,13,0.01,0.1])

    ### START  PIPELINE
    analysis.varscan_files()
    analysis.sam_tools_files(use_recal_files)
    check_create_dir(analysis.varscan_calls_dir)
    check_create_dir(analysis.sam_calls_dir)

    for sp in analysis.samples:
        if use_recal_files:
            if sp.category=='benign':
                normal_bam_file = sp.sorted_mmarkdup_bam                
            else:
                tumor_bam_file = sp.sorted_mmarkdup_bam
        else:
            if sp.category=='benign':
                normal_bam_file = sp.sorted_quickmmdup_bam
            else:
                tumor_bam_file = sp.sorted_quickmmdup_bam
    
    print analysis.benign_bcf_mpileup_file                       
    bcf_vars=analysis.bcf_vars_mpileup_file
    bcf_sam=analysis.bcf_sam_mpileup_file
    snps_sam=analysis.snps_sam
    snps_varscan = analysis.snps_varscan
    indels_varscan=analysis.indels_varscan
    
    job_results=[]
    ##### CALL SOMATIC MUTATIONS USING VARSCAN TOOL
    py_script = os.path.join(_jobs_dir, "snps_byVarScan_Somatic.py")
    args = [sys.executable, py_script,
            ref_genome,
            normal_bam_file,
            tumor_bam_file,
            snps_varscan,
            indels_varscan]
    print args
    
    logging.info("[RUN VARSCAN SOMATIC CALLS] Patient %s" % (analysis.name))
    job_name = ('%s_%s')%(analysis.name,"vs")
    job_id = runfunc(job_name, args, SINGLE_CORE, cwd=analysis.varscan_calls_dir, 
                     walltime=WT_LONG,
                     pmem=extra_mem,
                     stdout="vars_calls.log", 
                     deps=depends, 
                     email_addresses=my_email)
    job_results.append(job_id)
    
    ##### CALL SOMATIC MUTATIONS USING SAMTOOLS
    py_script = os.path.join(_jobs_dir, "snps_bySamtools_Somatic.py")
    args = [sys.executable, py_script,
            ref_genome,
            normal_bam_file,
            tumor_bam_file,
            bcf_sam,
            snps_sam]
    print args
    
    logging.info("[RUN SAMTOOLS SOMATIC CALLS] Patient %s" % (analysis.name))
    job_name = ('%s_%s')%(analysis.name,"st")
    job_id = runfunc(job_name, args, SINGLE_CORE, cwd=analysis.sam_calls_dir, 
                     walltime=WT_LONG,
                     pmem=extra_mem,
                     stdout="sam_calls.log", 
                     deps=depends, 
                     email_addresses=my_email)
    job_results.append(job_id)
    
    print job_results
    return job_results
    

    

if __name__ == '__main__':
        
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-r", "--config_file", dest="config_file",
                            help="file with run configuration")
    optionparser.add_option("-a", "--analysis_file", dest="analysis_file",
                            help="file with experiment configuration") 
    optionparser.add_option("--paired_samples", dest="paired_samples", action="store_true", default=False,
                            help="paired samples snv calling") 
    optionparser.add_option("--multi_samples", dest="multi_samples", action="store_true", default=False,
                            help="multi-sample snv calling") 
    optionparser.add_option("--local", dest="local", action="store_true", default=False)
    optionparser.add_option("--cluster", dest="cluster", action="store_true", default=False)
    optionparser.add_option("-p", "--processes", type=int, dest="num_processors", default=1)
    optionparser.add_option("--local_cluster", dest="local_cluster", action="store_true", default=False)


    (options, args) = optionparser.parse_args()    

    config = ExomePipelineConfig()
    config.from_xml(options.config_file)
    analysis = ExomeAnalysisConfig()
    analysis.from_xml(options.analysis_file, config.output_dir)
    # Default when called from the command line
    depends=None
    use_reacalibrated_bam=False
    
    if not (options.local ^ options.cluster ^ options.local_cluster):
        optionparser.error("Must set either --local, --cluster or --local_cluster to run job")
    if options.local:
        jobrunfunc = run_local
    elif options.cluster:
        jobrunfunc = qsub_cac
    elif options.local_cluster:
        jobrunfunc = qsub_loc

    snps_calling_pairedSamples(analysis, config, options.num_processors, jobrunfunc, 
                                      depends, use_reacalibrated_bam)
    
     
     


